package com.zwb.boot.common.plugins;

import com.fasterxml.classmate.TypeResolver;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.OperationBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.service.Operation;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ApiListingScannerPlugin;
import springfox.documentation.spi.service.contexts.DocumentationContext;
import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator;

import java.util.*;

/**
 * @author 头发又黑又长
 * @Date 2022/7/25 15:36
 * @email zwb15083976291@163.com
 */
@Profile({"dev"})
@Component
public class SwaggerAddtion implements ApiListingScannerPlugin {

    @Autowired(required = false)
    private ObjectMapper jsonMapper;


    @SneakyThrows
    @Override
    public List<ApiDescription> apply(DocumentationContext documentationContext) {
        return Arrays.asList(login(), logout());
    }


    public Parameter loginData() throws JsonProcessingException {
        return new ParameterBuilder()
                .description("登录(提供 username和 password)")
                .type(new TypeResolver().resolve(String.class))
                .name("loginData")
                .parameterType("body")
                .parameterAccess("access")
                .defaultValue("23")
                .required(true)
                .modelRef(new ModelRef("string"))
                .build();
    }


    /**
     * 定义用户名的字段
     *
     * @return
     */
    public Parameter username() {
        return new ParameterBuilder()
                .description("用户名")
                .type(new TypeResolver().resolve(String.class))
                .name("username")
                .parameterType("body")
                .parameterAccess("access")
                .defaultValue("admin")
                .required(true)
                .modelRef(new ModelRef("string"))
                .build();
    }

    public Parameter password() {
        return new ParameterBuilder()
                .defaultValue("密码")
                .type(new TypeResolver().resolve(String.class))
                .name("password")
                .parameterType("query")
                .parameterAccess("access")
                .defaultValue("123456")
                .required(true)
                .modelRef(new ModelRef("string"))
                .build();
    }


    /**
     * 定义消息返回
     *
     * @return
     */
    public Set<ResponseMessage> responseMessages() {
        Set<ResponseMessage> messages = new HashSet<>();
        messages.add(new ResponseMessageBuilder().code(200).message("ok").build());
        messages.add(new ResponseMessageBuilder().code(401).message("Unauthorized").build());
        messages.add(new ResponseMessageBuilder().code(403).message("Forbidden").build());
        messages.add(new ResponseMessageBuilder().code(404).message("Not Found").build());
        return messages;

    }

    /**
     * 登录请求
     *
     * @return
     */
    public ApiDescription login() throws JsonProcessingException {
        Operation loginOperation = new OperationBuilder(new CachingOperationNameGenerator())
                .tags(Collections.singleton("用户接口"))
                .summary("登录")
                .notes("登录")
                .method(HttpMethod.POST)
                .consumes(Collections.singleton(MediaType.APPLICATION_JSON_VALUE)) // 请求参数格式
                .produces(Collections.singleton(MediaType.APPLICATION_JSON_VALUE)) // 接口数据格式
//                .parameters(Arrays.asList(username(), password()))// 定于参数
                .parameters(Arrays.asList(loginData()))// 定于参数
                .responseMessages(responseMessages())
                .build();
        return new ApiDescription("login", "/employee/login", "登录接口",
                Collections.singletonList(loginOperation), false);

    }


    /**
     * 退出
     *
     * @return
     * @throws JsonProcessingException
     */
    public ApiDescription logout() {
        Operation logoutOperation = new OperationBuilder(new CachingOperationNameGenerator())
                .tags(Collections.singleton("用户接口"))
                .summary("登出")
                .notes("登出")
                .method(HttpMethod.GET)
                .responseMessages(responseMessages())
                .build();
        return new ApiDescription("logout", "/employee/logout", "等出接口",
                Collections.singletonList(logoutOperation), false);
    }


    /**
     * 是否使用此插件
     *
     * @param documentationType swagger文档类型
     * @return true 启用
     */
    @Override
    public boolean supports(DocumentationType documentationType) {
        return DocumentationType.SWAGGER_2.equals(documentationType);
    }
}
